package com.medicine.web.controller.statistics;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.manage.block.WeBASEUtils;
import com.medicine.manage.domain.vo.KeyValue;
import com.medicine.manage.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private WeBASEUtils weBASEUtils;

    @PostMapping("/orderCountAll")
    public AjaxResult orderCountAll(){
        Map<String, Integer> params = new HashMap<>();
        params.put("result", orderService.orderCountAll());
        return success(params);
    }

    @PostMapping("/orderCountOneDay")
    public AjaxResult orderCountOneDay(){
        Map<String, Integer> params = new HashMap<>();
        params.put("result", orderService.orderCountOneDay());
        return success(params);
    }

    @PostMapping("/orderCountForComplete")
    public AjaxResult orderCountForComplete(){
        Map<String, Integer> params = new HashMap<>();
        params.put("result", orderService.orderCountForComplete());
        return success(params);
    }

    @PostMapping("/orderCountForNotComplete")
    public AjaxResult orderCountForNotComplete(){
        Map<String, Integer> params = new HashMap<>();
        params.put("result", orderService.orderCountForNotComplete());
        return success(params);
    }

    @GetMapping("/getBlockNum")
    public AjaxResult getBlockNum(){
        Map<String, Integer> params = new HashMap<>();
        params.put("result", weBASEUtils.getBlockNum());
        return success(params);
    }

    @GetMapping("/getNodeStatusList")
    public AjaxResult getNodeStatusList(){

        String string = weBASEUtils.getNodeStatusList();

        System.out.println(string);
        return success(JSONArray.parseArray(string));
    }



    @PostMapping("orderCountForThirtyDay")
    public AjaxResult orderCountForThirtyDay(){
        JSONObject result = new JSONObject();
        List<KeyValue> queryList = orderService.orderCountForThirtyDay();
        List<String> keysList = new ArrayList();
        List<String> valuesList = new ArrayList();
        System.out.println(queryList);
        for (KeyValue keyValue : queryList) {
            keysList.add(keyValue.getK());
            valuesList.add(keyValue.getV());
        }
        result.put("keys", keysList);
        result.put("values", valuesList);
        return success(result);
    }
}
